#include "main.h"

void startSecretEditor()
{
	ifstream fin;
	ofstream fout;

	px.getS(gString,"Press enter to start your adventure...");
	if(gString=="fredisfat")
	{
		gInt=0;
		int	iTemp=0;
		gString.clear();
		gBool = false;

		while(!gBool)
		{
			px.nl(5);
			px.text("1.\tNew Weapon\n2.\tShow Weapon");
			px.text("3.\tNew Armor\n4.\tShow Armor");
			px.text("5.\tNew Item\n6.\tShow Item");
			px.text("10.\tExit Editor");
			px.getS(gString,"Enter your choice young programmer > ");
			ss.stringToNumber(gString,gInt);
			switch(gInt)
			{
			case 1: // New weapon
				gString.clear();
				fout.open("weapon.rpg", ios::app | ios::out);

				px.getS(gString,"Weapon name: ");
				fout << gString << ",";

				gString.clear();
				px.getS(gString,"Weapon desc: ");
				fout << gString << ",";
				
				iTemp = 0;
				px.getI(iTemp,"Weapon value: ");
				fout << iTemp << ",";

				iTemp = 0;
				px.getI(iTemp,"Weapon damage dice (6 for d6): ");
				fout << iTemp << ",";

				iTemp = 0;
				px.getI(iTemp,"Weapon damage dice number (2 for 2d6): ");
				fout << iTemp << ",";

				iTemp = 0;
				px.getI(iTemp,"Weapon critical range (18 for 18-20): ");
				fout << iTemp << ",";

				iTemp = 0;
				px.getI(iTemp,"Weapon critical modifier (2 for 2x): ");
				fout << iTemp << "#" << endl;

				fout.close();

				px.pause("Done...");
				break;

			case 2: // Parse weapon
				fin.open("weapon.rpg");

				if (fin.is_open())
				{
					while ( !fin.eof() )
					{
						getline (fin,gString,',');
						if(gString.empty()) // nothing left to read
							break;
						px.text("Weapon name: ",0);
						px.text(gString);

						gString.clear();
						getline (fin,gString,',');
						px.text("Weapon desc: ", false);
						px.text(gString);

						gString.clear();
						iTemp=0;
						getline (fin,gString,',');
						ss.stringToNumber(gString,iTemp);
						px.text("Weapon value: ",false);
						px.number(iTemp);

						gString.clear();
						getline (fin,gString,',');
						ss.stringToNumber(gString, gInt);
						px.text("Weapon damage dice: ", false);

						gString.clear();
						iTemp=0;
						getline (fin,gString,',');
						ss.stringToNumber(gString,iTemp);
						px.number(iTemp, false); // take number of dice after dice sides so this is not a typo
						px.text("d", false);
						px.number(gInt);

						// clean up
						gInt = 0;


						gString.clear();
						iTemp=0;
						getline (fin,gString,',');
						ss.stringToNumber(gString,iTemp);
						px.text("Weapon critical range: ", false);
						px.number(iTemp);


						gString.clear();
						iTemp=0;
						getline (fin,gString,'#');
						ss.stringToNumber(gString,iTemp);
						px.text("Weapon critical modifier: x", false);
						px.number(iTemp);
						px.nl(1);

						fin >> std::ws; // clear out trailing NL
					}
					fin.close();
				}
				break;

			case 3: // New armor
				gString.clear();
				fout.open("armor.rpg", ios::app | ios::out);

				px.getS(gString,"Armor name: ");
				fout << gString << ",";

				gString.clear();
				px.getS(gString,"Armor desc: ");
				fout << gString << ",";

				iTemp = 0;
				px.text("1 for leather, 2 for studded, 3 for hide, 4 for scale, 5 for bronze, 6 for iron, 7 for half plate, 8 for full plate.");
				px.getI(iTemp,"Armor type: ");
				fout << iTemp << ",";

				iTemp = 0;
				px.getI(iTemp,"Armor value: ");
				fout << iTemp << ",";

				iTemp = 0;
				px.getI(iTemp,"Armor protection ( 1 for -1 from damage ): ");
				fout << iTemp << ",";

				iTemp = 0;
				px.getI(iTemp,"Armor class ( 14 for protect for 14 and under hit roll ): ");
				fout << iTemp << "#" << endl;

				fout.close();

				px.pause("Done...");
				break;

				break;

			case 4: // Parse armor
				fin.open("armor.rpg");

				if (fin.is_open())
				{
					while ( !fin.eof() )
					{
						getline (fin,gString,',');
						if(gString.empty()) // nothing left to read
							break;
						px.text("Armor name: ", false);
						px.text(gString);

						gString.clear();
						getline (fin,gString,',');
						px.text("Armor desc: ", false);
						px.text(gString);

						gString.clear();
						getline (fin,gString,',');
						ss.stringToNumber(gString,gInt);
						px.text("0 for padded, 1 for leather, 2 for studded, 3 for hide, 4 for scale, 5 for chain,\n6 for iron, 7 for splint mail, 8 for half plate, 9 for full plate.");
						px.text("Armor type: ", false);

						gString.clear();
						iTemp=0;
						getline (fin,gString,',');
						ss.stringToNumber(gString,iTemp);
						px.number(iTemp);
	
						gString.clear();
						iTemp=0;
						getline (fin,gString,',');
						ss.stringToNumber(gString,iTemp);
						px.text("Armor value: ",false);
						px.number(iTemp);

						gString.clear();
						iTemp=0;
						getline (fin,gString,',');
						ss.stringToNumber(gString,iTemp);
						px.text("Armor protection ( 1 for -1 from damage ): ", false);
						px.text("-1",false);
						px.number(iTemp);


						gString.clear();
						iTemp=0;
						getline (fin,gString,'#');
						ss.stringToNumber(gString,iTemp);
						px.text("Armor class ( 14 for protect for 14 and under hit roll ): ", false);
						px.number(iTemp);
						px.nl(1);

						fin >> std::ws; // clear out trailing NL
					}
					fin.close();
				}
				break;

			case 5: // New item
				gString.clear();
				fout.open("item.rpg", ios::app | ios::out);

				px.getS(gString,"Item name: ");
				fout << gString << ",";

				gString.clear();
				px.getS(gString,"Item desc: ");
				fout << gString << ",";

				px.text("1 for hp, 2 for mp, 3 for thawing, 4 for antidote."); // TODO: Add more stuff to item list
				px.getI(iTemp,"Item type: ");
				fout << iTemp << "#" << endl;

				fout.close();

				px.pause("Done...");
				break;

			case 6: // Parse item

				fin.open("item.rpg");

				if (fin.is_open())
				{
					while ( !fin.eof() )
					{
						getline (fin,gString,',');
						if(gString.empty()) // nothing left to read
							break;
						px.text("Item name: ", false);
						px.text(gString);

						gString.clear();
						getline (fin,gString,',');
						px.text("Item desc: ", false);
						px.text(gString);

						gString.clear();
						iTemp=0;
						getline (fin,gString,'#');
						ss.stringToNumber(gString,iTemp);
						px.text("1 for hp, 2 for mp, 3 for thawing, 4 for antidote.");
						px.text("Item class: ", false);
						px.number(iTemp);
						px.nl(1);

						fin >> std::ws; // clear out trailing NL
					}
					fin.close();
				}
				break;

			case 10: // Exit
				gBool = true;
				gDone = true;
				break;

			default:
				px.pause("Invalid choice foolish programmer...");
				break;

			}
		}
	}
}